package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.data.OrderData;
import com.cskaoyan.bean.vo.UserVo;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class DetailServiceImpl implements DetailService {
    @Override
    public BaseRespVo detail(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);
        MarketOrderGoodsMapper marketOrderGoodsMapper = sqlSession.getMapper(MarketOrderGoodsMapper.class);
        MarketUserMapper userMapper = sqlSession.getMapper(MarketUserMapper.class);

        MarketOrderExample example = new MarketOrderExample();
        MarketOrderGoodsExample example1 = new MarketOrderGoodsExample();
        MarketUserExample example2 = new MarketUserExample();

        MarketOrderExample.Criteria criteria = example.createCriteria();
        MarketOrderGoodsExample.Criteria criteria1=example1.createCriteria();
        MarketUserExample.Criteria criteria2 = example2.createCriteria();

        criteria.andIdEqualTo(id);
        List<MarketOrder> marketOrders = orderMapper.selectByExample(example);
        MarketOrder marketOrder = marketOrders.get(0);


        criteria1.andOrderIdEqualTo(id);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(example1);



        criteria2.andIdEqualTo(marketOrder.getUserId());
        List<MarketUser> marketUsers = userMapper.selectByExample(example2);
        MarketUser marketUser = marketUsers.get(0);
        UserVo userVo = new UserVo();
        userVo.setNickname(marketUser.getNickname());
        userVo.setAvatar(marketUser.getAvatar());


        PageInfo pageInfo = new PageInfo(marketOrderGoods);
        sqlSession.close();// 不要忘记
        return BaseRespVo.ok(OrderData.data(marketOrder,pageInfo,userVo));
    }
}
